package cn.mst.vrs.service.authtype;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypePageReqVO;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypeSaveReqVO;
import cn.mst.vrs.dal.dataobject.authtype.AuthTypeDO;
import cn.mst.vrs.dal.mysql.authtype.AuthTypeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mst.vrs.enums.ErrorCodeConstants.AUTH_TYPE_NOT_EXISTS;

/**
 * 认证类型 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class AuthTypeServiceImpl implements AuthTypeService {

    @Resource
    private AuthTypeMapper authTypeMapper;

    @Override
    public Long createAuthType(AuthTypeSaveReqVO createReqVO) {
        // 插入
        AuthTypeDO authType = BeanUtils.toBean(createReqVO, AuthTypeDO.class);
        authTypeMapper.insert(authType);
        // 返回
        return authType.getId();
    }

    @Override
    public void updateAuthType(AuthTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateAuthTypeExists(updateReqVO.getId());
        // 更新
        AuthTypeDO updateObj = BeanUtils.toBean(updateReqVO, AuthTypeDO.class);
        authTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteAuthType(Long id) {
        // 校验存在
        validateAuthTypeExists(id);
        // 删除
        authTypeMapper.deleteById(id);
    }

    private void validateAuthTypeExists(Long id) {
        if (authTypeMapper.selectById(id) == null) {
            throw exception(AUTH_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public AuthTypeDO getAuthType(Long id) {
        return authTypeMapper.selectById(id);
    }

    @Override
    public PageResult<AuthTypeDO> getAuthTypePage(AuthTypePageReqVO pageReqVO) {
        return authTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AuthTypeDO> listAuthTypeDict() {
        return authTypeMapper.selectDictList();
    }

}